package com.hs.mobile.dao.register;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.hs.mobile.model.Device;
import com.hs.mobile.model.Register;

public class RegisterDAOImpl implements RegisterDAO {

	SessionFactory sessionFactory;	
	//setter sessionFactory
	/**
	 * 
	 * @param sessionFactory
	 */
	public void setSessionFactory( SessionFactory sessionFactory )
	{
		this.sessionFactory = sessionFactory;
	}
	
	//getter sessionFactory
	/**
	 * 
	 * @return sessionFactory
	 */
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;		
	}
	
	@Override
	public List getListAll() {
		// TODO Auto-generated method stub
		System.out.println("__ get list All Register __ ");
		Session session = getSessionFactory().openSession();
		List<Register> listRegisters = null;
		try {
			Criteria cr = session.createCriteria(Register.class);
			listRegisters = cr.list();
			System.out.println("__ get list Registers __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listRegisters;
	}

	@Override
	public Object getById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get register __");
		Session session = getSessionFactory().openSession();
		Register register = null;
		try {
			register = (Register) session.get(Register.class, id);
			System.out.println("__ Get register successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return register;
	}

	@Override
	public void add(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Register  __ ");
		Register register = ( Register ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.save(register);
			session.getTransaction().commit();
			System.out.println("__ register successfully __");
			System.out.println("__ Your mobile can use this service from now __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Erro __");
		} finally {
			session.close();
		}	
	}

	@Override
	public void delete(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Your mobile does not continue using this service ___");
		Register register = (Register) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(register);
			session.getTransaction().commit();
			System.out.println("__ Delete device successful __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public void update(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Update __ ");
		Register register = (Register) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.update(register);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public Register findByRegisterId(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get register __");
		Session session = getSessionFactory().openSession();
		Register register = null;
		try {
			register = (Register) session.get(Register.class, id);
			System.out.println("__ Get register successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return register;
	}

	@Override
	public List<Register> getAllRegisterByDeviceId(long deviceId) {
		// TODO Auto-generated method stub
		System.out.println("__ get list All Register __ ");
		Session session = getSessionFactory().openSession();
		List<Register> listRegisters = null;
		try {
			String sql = " SELECT * FROM REGISTER WHERE ID_DEVICE = " + deviceId;
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Register.class);
	        listRegisters = query.list();
			System.out.println("__ get list Registers __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listRegisters;
	}

	@Override
	public List<Register> getAllRegisterByDeviceIdandCategoryId(long deviceId,
			long categoryId) {
		// TODO Auto-generated method stub
		System.out.println("__ get list All Register by DeviceId and CategoryId __ ");
		Session session = getSessionFactory().openSession();
		List<Register> listRegisters = null;
		try {
			String sqlCommand = "SELECT *FROM REGISTER INNER JOIN DEVICE ON DEVICE.ID = REGISTER.ID_DEVICE WHERE REGISTER.ID_CATEGORY =  " + categoryId;
			SQLQuery query = session.createSQLQuery( sqlCommand );
	        query.addEntity(Register.class);
	        listRegisters = query.list();
			System.out.println("__ get list Registers __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listRegisters;
	}

}
